<template>
  <div class="box-content">
    <div class="navigation">设备分类列表/子分类列表<span class="check" @click="$router.go(-1)">返回上一级</span></div>
    <!--搜索-->
    <div class="mt-3">
      <el-row :gutter="10">
        <el-col :span="6">
          <el-input type="text" size="small" v-model="search.inputName" :clearable="true" placeholder="输入关键字(设备分类名)"></el-input>
        </el-col>
        <el-col :span="18">
          <el-button type="primary" size="small" @click="onSearch">查询</el-button>
          <el-button type="primary" size="small" @click="createAccount">新增</el-button>
        </el-col>
      </el-row>
    </div>
    <!--表格-->
    <div class="mt-3">
      <el-table
              :data="userData"
              v-loading="loading"
              align="center"
              :element-loading-text="loadText"
              :header-cell-style="tableHeaderColor"
              stripe
              style="width: 100%">
        <el-table-column
                prop="id"
                align="center"
                label="编号"
                width="100"
        >
        </el-table-column>
        <el-table-column
                prop="dvc_name"
                align="center"
                label="子分类名"
        >
        </el-table-column>
        <el-table-column
                prop="dvc_desc"
                align="center"
                label="子分类描述"
        >
        </el-table-column>
        <el-table-column
                label="创建时间"
                align="center"
                width="100">
          <template slot-scope="scope">
            <div align="center">{{scope.row['created_at']?scope.row['created_at'].slice(0, 11):'无'}}</div>
            <div align="center">{{scope.row['created_at']?scope.row['created_at'].slice(11 ):'无'}}</div>
          </template>
        </el-table-column>
        <el-table-column
                label="操作"
                align="center"
                fixed="right"
                width="160">
          <template slot-scope="scope">
            <el-button
                    size="small"
                    type="primary"
                    @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button
                    size="small"
                    type="danger"
                    @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt-3 float-right">
        <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageInfo.current_page"
                :page-size="pageInfo.per_page"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageInfo.total">
        </el-pagination>
      </div>
    </div>
    <!--新增对话框-->
    <el-dialog :title="dialog.title"
               :visible.sync="dialogFormVisible"
               :close-on-click-modal="false"
               width="600px">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px" v-loading="loadingDialig">
        <el-form-item label="子分类名" prop="dvc_name">
          <el-input v-model="form.dvc_name" placeholder="请输入子分类名" style="width: 450px;"></el-input>
        </el-form-item>
        <el-form-item label="子分类描述" prop="dvc_desc">
          <el-input v-model="form.dvc_desc" placeholder="请输入子分类描述" style="width: 450px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" v-if="!dialog.isEdit" :loading="loadingCreate"
                   @click="submitCostomer('form')">{{loadingCreate?'创建中':'点击创建'}}
        </el-button>
        <el-button type="primary" v-if="dialog.isEdit" :loading="loadingCreate" @click="submitCostomer('form')">
          {{loadingCreate?'修改中':'点击修改'}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

    export default {
        name: "fenList",
        data() {
            return {
                baseId: null,
                pageInfo: {
                    per_page: 10,
                    total: 0,
                    current_page: 1
                },
                loadText: '数据加载中',
                userData: [],  // 设备列表
                dvcData: [], // 设备分类数据
                searchStr: '',
                search: {
                    inputName: ''
                },
                loading: false,
                loadingDialig: false,
                loadingCreate: false,
                dialogFormVisible: false,
                form: {
                    dvc_name: '',
                    dvc_desc: ''
                },
                dialog: {
                    title: '',
                    isEdit: false
                },
                rules: {
                    dvc_name: [
                        {required: 'true', message: '请输入子分类名', trigger: 'change'}
                    ],
                    dvc_desc: [
                        {required: 'true', message: '请输入子分类描述', trigger: 'change'}
                    ]
                }
            }
        },
        watch: {
            dialogFormVisible: function (val) {
                if (!val) {
                    Object.keys(this.form).forEach(key => this.form[key] = '');
                    this.$refs['form'].resetFields();
                }
            }
        },
        created() {
            this.getAccountList();
        },
        methods: {
            changeName(val) {
                this.search.inputName = val;
            },
            createAccount() {
                this.dialog.title = '添加子分类';
                this.dialog.isEdit = false;
                this.dialogFormVisible = true;
            },
            getAccountList() {
                this.loading = true;
                this.$http.get('api/auth/deviceCategory?parent_id=' + this.$route.params.id + this.searchStr, {
                    params: {
                        pageSize: this.pageInfo.per_page,
                        page: this.pageInfo.current_page
                    }
                })
                    .then((result) => {
                        if (result.status_code == 200) {
                            this.userData = result.data.data;
                            this.pageInfo.total = result.data.total;
                        } else {
                            this.$message.warning(result.msg);
                        }
                        this.loading = false;
                    })
                    .catch((err) => {
                        this.loading = false;
                        this.$message.warning(err.data.msg || "数据异常，请重试");
                    })
            },
            onSearch() {
                this.searchStr = "";
                if (this.search.inputName) {
                    // this.addConStr();
                    this.searchStr += `&dvc_name=${this.search.inputName}`;
                }
                this.pageInfo.current_page = 1;
                this.getAccountList();
            },
            addConStr() {
                if (this.searchStr.length > 0) {
                    this.searchStr += '&';
                } else {
                    this.searchStr += '?';
                }
            },
            submitCostomer(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.creatShanHu();
                    } else {
                        return false;
                    }
                });
            },
            creatShanHu() {
                this.loadingCreate = true;
                let params;
                if (this.dialog.isEdit) {
                    params={
                        dvc_name:this.form.dvc_name,
                        dvc_desc:this.form.dvc_desc
                    };
                    this.$http.put('api/auth/deviceCategory/' + this.baseId, params)
                        .then((result) => {
                            if (result.status_code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: result.msg,
                                    during: 1000
                                });
                                this.getAccountList();
                                this.dialogFormVisible = false;
                            } else {
                                this.$message.warning(result.msg);
                            }
                            this.loadingCreate = false;
                        })
                        .catch((err) => {
                            this.loadingCreate = false;
                            this.$message.warning(err.data.msg || "数据异常，请重试");
                        })
                } else {
                    params={
                        dvc_name:this.form.dvc_name,
                        dvc_desc:this.form.dvc_desc,
                        parent_id: this.$route.params.id
                    };
                    this.$http.post('api/auth/deviceCategory', params)
                        .then((result) => {
                            if (result.status_code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: result.msg,
                                    during: 1000
                                });
                                this.getAccountList();
                                this.dialogFormVisible = false;
                            } else {
                                this.$message.warning(result.msg);
                            }
                            this.loadingCreate = false;

                        })
                        .catch((err) => {
                            this.loadingCreate = false;
                            this.$message.warning(err.data.msg || "数据异常，请重试");
                        })
                }

            },
            handleEdit(val) {
                this.baseId = val.id;
                this.dialog.title = '编辑子分类';
                this.dialog.isEdit = true;
                this.dialogFormVisible = true;
                this.loadingDialig = true;
                this.$http.get('api/auth/deviceCategory/' + val.id)
                    .then((result) => {
                        if (result.status_code == 200) {
                            this.form.dvc_name = result.data.dvc_name,
                            this.form.dvc_desc = result.data.dvc_desc
                        } else {
                            this.$message.warning(result.msg);
                        }
                        this.loadingDialig = false;
                        this.loadingCreate = false;
                    })
                    .catch((err) => {
                        this.loadingDialig = false;
                        this.$message.warning(err.data.msg || "数据异常，请重试");
                    })
            },
            handleDelete(val) {
                this.$confirm('此操作将删除此子分类, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.delete('api/auth/deviceCategory/' + val.id)
                        .then((result) => {
                            if (result.status_code == 200) {
                                this.$message({
                                    type: 'success',
                                    message: result.msg,
                                    during: 1000
                                });
                                this.getAccountList();
                            } else {
                                this.$message.warning(result.msg);
                            }
                        })
                        .catch((err) => {
                            this.$message.warning(err.data.msg || "数据异常，请重试");
                        })
                }).catch(() => {
                });
            },
            handleSizeChange(val) {
                this.pageInfo.current_page = 1;
                this.pageInfo.per_page = val;
                this.getAccountList();
            },
            handleCurrentChange(val) {
                this.pageInfo.current_page = val;
                this.getAccountList();
            },
            tableHeaderColor({row, column, rowIndex, columnIndex}) {
                if (rowIndex === 0) {
                    return 'background-color: #FAFAFA;color: #000;font-weight: 500;height:54px;font-size:14px'
                }
            },
        }
    }
</script>

<style lang="less" scoped>
</style>